package com.crm.service.impl.sys.permissionManagement;

import com.crm.common.model.Enum.StatusCodeEnum;
import com.crm.common.util.CommonUtils;
import com.crm.dao.mapper.sys.permissionManagement.SysPermissionGroupMapper;
import com.crm.model.entity.sys.UserAccount;
import com.crm.model.entity.sys.permissionManagement.SysAccountPermission;
import com.crm.model.entity.sys.permissionManagement.SysPermissionGroup;
import com.crm.model.entity.sys.permissionManagement.SysPermissionGroupExample;
import com.crm.service.sys.permissionManagement.SysAccountPermissionDbService;
import com.crm.service.sys.permissionManagement.SysPermissionGroupDbService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.apache.shiro.SecurityUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author yincuimei
 * @Date 2020/12/6 19:32
 */
@Service
public class SysPermissionGroupDbServiceImpl implements SysPermissionGroupDbService {

    @Autowired
    private SysPermissionGroupMapper sysPermissionGroupMapper;

    @Autowired
    private SysAccountPermissionDbService sysAccountPermissionDbService;

    /**
     * 新增一条权限组数据
     * @param sysPermissionGroup ---权限组信息
     * @return
     */
    @Override
    public Boolean insertSysPermissionGroup(SysPermissionGroup sysPermissionGroup) {
        return sysPermissionGroupMapper.insert(sysPermissionGroup) > 0;
    }

    @Override
    public List<SysPermissionGroup> selectPermissionManagementInfo(String searchInfo) {
        UserAccount account = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        List<SysPermissionGroup> permissionGroups = null;
        if(account != null){
            SysPermissionGroupExample sysPermissionGroupExample = new SysPermissionGroupExample();
            SysPermissionGroupExample.Criteria criteria = sysPermissionGroupExample.createCriteria();
            criteria.andDeleteStatusEqualTo(Short.parseShort("0"));
            if (StringUtils.isNotEmpty(searchInfo)) {
                criteria.andPermissionGroupNameLike("%" + searchInfo.trim() + "%");
            }
            sysPermissionGroupExample.setOrderByClause("create_tm desc");
            boolean isAdmin = CommonUtils.isAdmin(account.getUserId());
            if(isAdmin){
                permissionGroups = sysPermissionGroupMapper.selectByExample(sysPermissionGroupExample);
            }else{//非超级管理员 根据账号查和权限组的关联表
                String accountId = account.getAccountId();
                List<SysAccountPermission> permissionGroupsInfo = sysAccountPermissionDbService.findPermissionGroupsInfo(accountId);
                if(permissionGroupsInfo.size()>0 && null != permissionGroupsInfo){
                    List<String> list = new ArrayList<>();
                    for(int i=0;i<permissionGroupsInfo.size();i++){
                        String permissionGroupId = permissionGroupsInfo.get(i).getPermissionGroupId();
                        list.add(permissionGroupId);
                    }
                    criteria.andPermissionGroupIdIn(list);
                    permissionGroups = sysPermissionGroupMapper.selectByExample(sysPermissionGroupExample);
                }
             }
        }

        return permissionGroups;
    }

    @Override
    public Boolean deleteSysPermissionGroupById(String permissionGroupId) {
        SysPermissionGroup sysPermissionGroup = new SysPermissionGroup();
        sysPermissionGroup.setPermissionGroupId(permissionGroupId);
        sysPermissionGroup.setDeleteStatus(Short.parseShort(String.valueOf(StatusCodeEnum.DELETE_CODE.getState())));
        return sysPermissionGroupMapper.updateByPrimaryKeySelective(sysPermissionGroup) > 0;
    }

    @Override
    public SysPermissionGroup selectSysPermissionGroupById(String permissionGroupId) {
        return sysPermissionGroupMapper.selectByPrimaryKey(permissionGroupId);
    }

    /**
     * 更新权限组数据
     * @Author pym
     * @Date  2021/9/1 10:27
     * @param sysPermissionGroup 权限组信息
     * @return {@link {java.lang.Boolean}}
     */
    @Override
    public Boolean updateSysPermissionGroup(SysPermissionGroup sysPermissionGroup) {
        return sysPermissionGroupMapper.updateByPrimaryKey(sysPermissionGroup) > 0;
    }

}
